package com.example.travel_front.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.travel_front.Adapter.CalendarAdapter;
import com.example.travel_front.Adapter.HourlyForecastAdapter;
import com.example.travel_front.Model.CalendarDay;
import com.example.travel_front.Model.HourlyForecast;
import com.example.travel_front.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class IndexFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_index, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Initialize your forecast list here
        List<HourlyForecast> forecastList = getHourlyForecasts(); // This should return List<HourlyForecast>

        RecyclerView recyclerView = view.findViewById(R.id.hourlyForecastRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        HourlyForecastAdapter adapter = new HourlyForecastAdapter(forecastList);
        recyclerView.setAdapter(adapter);
        // Load MapFragment
        loadMapFragment();
        // Assuming you have a reference to the service card view
        View serviceCardView = view.findViewById(R.id.layout_service); // Adjust the ID as per your layout

        // Set up the TextViews from the service card
        TextView nursingHomeLabel = serviceCardView.findViewById(R.id.nursingHomeLabel);
        TextView homeServiceLabel = serviceCardView.findViewById(R.id.homeServiceLabel);

        nursingHomeLabel.setOnClickListener(v -> navigateToFragment(new HomeFragment()));
        homeServiceLabel.setOnClickListener(v -> navigateToFragment(new CaregiverFragment()));
        RecyclerView calendarRecyclerView = view.findViewById(R.id.recyclerView_calendar);
        calendarRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 7)); // 7 for a week

        List<CalendarDay> days = generateCalendarDays(); // Your method to generate a list of days
        CalendarAdapter calendarAdapter = new CalendarAdapter(days);
        calendarRecyclerView.setAdapter(calendarAdapter);


    }

    private List<HourlyForecast> getHourlyForecasts() {
        List<HourlyForecast> forecastList = new ArrayList<>();

        // Adding some sample data
        forecastList.add(new HourlyForecast("6 AM", R.drawable.ic_fine, "20°C"));
        forecastList.add(new HourlyForecast("7 AM", R.drawable.ic_cloudy, "21°C"));
        forecastList.add(new HourlyForecast("8 AM", R.drawable.ic_cloudy, "22°C"));
        forecastList.add(new HourlyForecast("9 AM", R.drawable.ic_cloudy, "18°C"));
        forecastList.add(new HourlyForecast("10 AM", R.drawable.ic_blizzard, "17°C"));
        forecastList.add(new HourlyForecast("11 AM", R.drawable.ic_cloudy, "18°C"));
        forecastList.add(new HourlyForecast("12 AM", R.drawable.ic_blizzard, "17°C"));

        return forecastList;
    }

    private void navigateToFragment(Fragment fragment) {
        FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, fragment); // Replace 'fragment_container' with your actual container ID
        transaction.addToBackStack(null); // This allows navigating back
        transaction.commit();
    }


    private void loadMapFragment() {
        // Assuming you have a MapFragment class
        Fragment mapFragment = new MapFragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.mapView, mapFragment).commit(); // Replace R.id.mapView with your actual MapView placeholder's ID
    }

    private List<CalendarDay> generateCalendarDays() {
        List<CalendarDay> days = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();

        // Set to the first day of the current month
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        int monthBeginningCell = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        // Adjust for week start (e.g., if week starts on Sunday, no adjustment needed)
        int weekStart = Calendar.SUNDAY;
        if (weekStart != Calendar.SUNDAY) {
            monthBeginningCell -= weekStart - Calendar.SUNDAY;
            if (monthBeginningCell < 0) {
                monthBeginningCell += 7;
            }
        }

        // Fill in leading month days
        for (int i = 0; i < monthBeginningCell; i++) {
            days.add(new CalendarDay(0, false));
        }

        // Fill in the days of the month
        for (int i = 1; i <= daysInMonth; i++) {
            days.add(new CalendarDay(i, true));
        }

        // Fill in the trailing days to complete the weeks
        int totalCells = (days.size() % 7 == 0) ? days.size() : (days.size() + 7 - (days.size() % 7));
        for (int i = daysInMonth + 1; days.size() < totalCells; i++) {
            days.add(new CalendarDay(0, false));
        }

        return days;
    }

}
